<?php
class banner_model extends CI_Model
{
	public function __construct()
	{
        parent::__construct();
	}
	/*===============Banner main====================*/
	function getAllBanner($num,$offset)
	{
		$this->db->order_by('BannerID','DESC');
		$query = $this->db->get('bannermain');
		return $query->result();
	}
	
	function getNumBanner()
	{
		$query = $this->db->get('bannermain');
		return $query->num_rows();
	}
	
	function get_write_banner()
	{
		$this->db->where('bl_active',1);
		//$this->db->where('TownID',$town);
		$this->db->order_by('BannerID','DESC');
		$query = $this->db->get('bannermain',5,0);
		return $query->result();
	}
	
	function saveBanner($BannerImg)
	{
	  $id = (int) $this->uri->segment(3);
	  $data = array(
		'BannerTitle' 	=> $this->input->post('BannerTitle'),
		'BannerImg' 	=> $BannerImg,
		'BannerLink' 	=> $this->input->post('BannerLink'),
		'idBannerCat' 	=> $this->input->post('ParentID'),
		//'TownID' 		=> (int) $this->input->post('TownID'),
		'bl_active' 	=> $this->input->post('bl_active')
	  );
	  
	  if($id!=0){
		  $this->db->where('BannerID',$id);
		  if($this->db->update('bannermain',$data)){
			  return true;
		  }else{
			  return false;
		  }
	  }else{
		  if($this->db->insert('bannermain',$data)){
			  return true;
		  }else{
			  return false;
		  }
	  }
	}
	
	function get_banner_by_id($id){
	  $this->db->where('BannerID',$id);
	  $query = $this->db->get('bannermain');
	  return $query->row();
	}
	
	function delete($id)
	{
		$this->db->where('BannerID',$id);
		$query = $this->db->delete('bannermain');
		if($query){
			return true;
		}else{
			return false;
		}
	}   
	
	function getListCat()
	{
		$this->db->where('bl_active',1);
		return $this->db->get('getListCat')->result();
	}

	/*===============Banner right ====================*/
	function getAllBanner_right($num,$offset)
	{
		$this->db->order_by('BannerID','DESC');
		$query = $this->db->get('banner_right');
		return $query->result();
	}
	
	function getNumBanner_right()
	{
		$query = $this->db->get('banner_right');
		return $query->num_rows();
	}
	
	
	function saveBanner_right($BannerImg)
	{
	  $id = (int) $this->uri->segment(3);
	  $data = array(
		'BannerTitle' 	=> $this->input->post('BannerTitle'),
		'BannerImg' 	=> $BannerImg,
		'BannerLink' 	=> $this->input->post('BannerLink'),
		//'TownID' 		=> (int) $this->input->post('TownID'),
		'bl_active' 	=> $this->input->post('bl_active')
	  );
	  
	  if($id!=0){
		  $this->db->where('BannerID',$id);
		  if($this->db->update('banner_right',$data)){
			  return true;
		  }else{
			  return false;
		  }
	  }else{
		  if($this->db->insert('banner_right',$data)){
			  return true;
		  }else{
			  return false;
		  }
	  }
	}
	
	function get_banner_right_by_id($id){
	  $this->db->where('BannerID',$id);
	  $query = $this->db->get('banner_right');
	  return $query->row();
	}
	
	function delete_banner_right($id)
	{
		$this->db->where('BannerID',$id);
		$query = $this->db->delete('banner_right');
		if($query){
			return true;
		}else{
			return false;
		}
	}  
	
	/*===============Banner bottom ====================*/
	function getAllBanner_bottom($num,$offset)
	{
		$this->db->order_by('BannerID','DESC');
		$query = $this->db->get('banner_bottom');
		return $query->result();
	}
	
	function getNumBanner_bottom()
	{
		$query = $this->db->get('banner_bottom');
		return $query->num_rows();
	}
	
	
	function saveBanner_bottom($BannerImg)
	{
	  $id = (int) $this->uri->segment(3);
	  $data = array(
		'BannerTitle' 	=> $this->input->post('BannerTitle'),
		'BannerImg' 	=> $BannerImg,
		'BannerLink' 	=> $this->input->post('BannerLink'),
		//'TownID' 		=> (int) $this->input->post('TownID'),
		'bl_active' 	=> $this->input->post('bl_active')
	  );
	  
	  if($id!=0){
		  $this->db->where('BannerID',$id);
		  if($this->db->update('banner_bottom',$data)){
			  return true;
		  }else{
			  return false;
		  }
	  }else{
		  if($this->db->insert('banner_bottom',$data)){
			  return true;
		  }else{
			  return false;
		  }
	  }
	}
	
	function get_banner_bottom_by_id($id){
	  $this->db->where('BannerID',$id);
	  $query = $this->db->get('banner_bottom');
	  return $query->row();
	}
	
	function delete_banner_bottom($id)
	{
		$this->db->where('BannerID',$id);
		$query = $this->db->delete('banner_bottom');
		if($query){
			return true;
		}else{
			return false;
		}
	}      
}
?>
